﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;

namespace WindowsForms.PBKT
{
	public partial class UCLichThiDau : UserControl
	{
		public LichThiDau ltd;
		public static bool state = false;
		public UCLichThiDau()
		{
			InitializeComponent();
			dgvLTD.AutoGenerateColumns = false;
		}

		private void btnLoad_Click(object sender, EventArgs e)
		{
			LoadLichThiDau(LichThiDau.Objects.All().ToList());
		}

		public void LoadLichThiDau(List<LichThiDau> list)
		{
			dgvLTD.DataSource = list;
			// dgvLTD.DataSource = LichThiDau.Objects.Find(x => x.MaDoi1 == "CTK33").ToList();

		}

		private void dgvLTD_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 7)
				{
					ltd = LichThiDau.Objects.GetById(dgvLTD.Rows[e.RowIndex].Cells[0].Value.ToString());
					state = true;
					FormThemTTLichThiDau frm = new FormThemTTLichThiDau(ltd);
										frm.ShowDialog();
					frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
					btnLoad.PerformClick();
					ltd = null;
				}
				if (e.ColumnIndex == 6)
				{
					ltd = LichThiDau.Objects.GetById(dgvLTD.Rows[e.RowIndex].Cells[0].Value.ToString());
					try
					{ 

						if (LichThiDau.Objects.Delete(ltd))
							MessageBox.Show("Xóa thành công");
						btnLoad.PerformClick();
					}

					catch (Exception)
					{

						//MessageBox.Show("Bạn phải xóa trong thông tin trận đấu trước");

						DialogResult rss = MessageBox.Show("Bạn phải xóa trong thông tin trận đấu trước", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
						if (rss == DialogResult.Yes)
						{
							ltd = LichThiDau.Objects.GetById(dgvLTD.Rows[e.RowIndex].Cells[0].Value.ToString());
							state = true;
							FormThemTTLichThiDau frm = new FormThemTTLichThiDau(ltd);

							frm.ShowDialog();
							frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
							ltd = null;
						}

					}
					ltd = null;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				
			}

			
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			FormThemTTLichThiDau frm = new FormThemTTLichThiDau(ltd);
			frm.ShowDialog();
			frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
			btnLoad.PerformClick();
		}

		void frm_FormClosed(object sender, FormClosedEventArgs e)
		{
			LoadLichThiDau(LichThiDau.Objects.All().ToList());
		}

		private void btnTimKiem_Click(object sender, EventArgs e)
		{
			FormTKLichThiDau frm = new FormTKLichThiDau();
			frm.ShowDialog();
			List<LichThiDau> ltDFind = LichThiDau.Objects.Find(x => x.ThoiGian== frm.thoiGian || x.DiaDiem == frm.diaDiem).ToList();
			dgvLTD.DataSource = ltDFind;
		}

		private void btnFind_Click(object sender, EventArgs e)
		{
			FormTrongTaiTKLichThiDau frm = new FormTrongTaiTKLichThiDau();
			frm.ShowDialog();
		}





	}
}
